/**
 * @file EnhancedAgent.cpp
 * @brief Implementation of enhanced economic agent with safety checks
 */

#include "EnhancedAgent.h"
#include <stdexcept>
#include <iostream>
#include <algorithm>

// [Previous implementations...]

void EnhancedAgent::updateGoals() {
    try {
        for (auto& goal : goals) {
            // Progress increases faster when working
            double increment = (currentState == AgentState::WORKING) ? 
                0.01 * knowledge : 0.002 * knowledge;
            
            goal.progress = std::clamp(goal.progress + increment, 0.0, 1.0);
            
            // Adjust priority based on progress
            goal.priority = std::max(0.1, goal.priority * (1.0 - goal.progress/2));
        }
    } catch(const std::exception& e) {
        std::cerr << "Goal update failed: " << e.what() << std::endl;
        throw;
    }
}

void EnhancedAgent::evaluateOptions() {
    if(goals.empty()) {
        throw std::runtime_error("No goals defined for evaluation");
    }

    try {
        // Sort goals by priority (highest first)
        std::sort(goals.begin(), goals.end(),
            [](const AgentGoal& a, const AgentGoal& b) {
                return a.priority > b.priority;
            });
        
        // TODO: Add actual option evaluation logic
        // Placeholder for decision weights calculation
    } catch(const std::exception& e) {
        std::cerr << "Option evaluation failed: " << e.what() << std::endl;
        throw;
    }
}

void EnhancedAgent::selectBestAction() {
    try {
        if(goals.empty()) {
            currentState = AgentState::IDLE;
            return;
        }

        // Simple heuristic: Focus on top priority goal
        const auto& primaryGoal = goals.front();
        
        if(primaryGoal.progress < 0.8) {
            currentState = AgentState::WORKING;
        } else if(knowledge < 0.7) {
            currentState = AgentState::LEARNING;
        } else {
            currentState = AgentState::SOCIALIZING;
        }
        
    } catch(const std::exception& e) {
        std::cerr << "Action selection failed: " << e.what() << std::endl;
        currentState = AgentState::IDLE;
        throw;
    }
}